package com.mars.basic.service;

import com.mars.basic.allmain.entity.AdminUserEntity;
import com.mars.basic.allmain.po.AdminUser;
import com.mars.basic.allmain.po.QAdminUser;
import com.mars.basic.allmain.vo.AdminUserVo;
import com.mars.basic.factory.AdminUserFac;
import com.mars.basic.repository.AdminUserRepository;
import com.mars.common.utils.Beans;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
@RequiredArgsConstructor(onConstructor =@__(@Autowired))
public class AdminUserService {

    final AdminUserRepository userRepository;

    public AdminUserRepository getAdminUserRepository(){
        return userRepository;
    }

    public boolean checkIsExists(AdminUserVo vo){
        boolean exists = getAdminUserRepository().exists(QAdminUser.adminUser.name.eq(vo.getName()));
      return !exists;
    }

    public void addAdmin(AdminUserVo dto){
        boolean exists = getAdminUserRepository().exists(QAdminUser.adminUser.name.eq(dto.getName()));
        if (!exists){
            AdminUserEntity save = getAdminUserRepository().save(AdminUserFac.toEntity(dto));
        }
    }

    public AdminUser findByUserName(String name){
        AdminUserEntity admin = getAdminUserRepository().findOneIfMutil(QAdminUser.adminUser.name.eq(name));
        AdminUser user = Beans.mapper(admin, AdminUser.class);
        return user;
    }
    /**
     * 逻辑需要增强不能删除自己
     * @param id
     * @return
     */
    public void deleteAdmin(Integer id){
        getAdminUserRepository().deleteById(id);
    }

    public AdminUserEntity findById(Integer id){
        AdminUserEntity adminUserEntity = getAdminUserRepository().findById(id).orElse(null);
        return adminUserEntity;
    }
}
